package com.lcm.learn.lcmcode.bitmap;

import java.io.*;
import java.util.BitSet;

/**
 * @description:
 * @author: lcm
 * @create: 2019-09-26 17:22
 **/

public class BitSortData {

    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(5000L);
        System.out.println("最大可用内存(MB)："+Runtime.getRuntime().maxMemory()/(1024*1024));
        sort("E:\\data\\data.txt","E:\\data\\data2.txt");

        get("E:\\data\\data.txt",0,100);
        System.out.println("---------------------------------");
        get("E:\\data\\data2.txt",0,100);
    }

    static void sort(String from,String to){
        Long start=System.currentTimeMillis();
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(from));
            writer = new BufferedWriter(new FileWriter(to));
            BitSet bitSet=new BitSet(100000000);
            String str = null;
            while ((str=reader.readLine())!=null){
                bitSet.set(Integer.parseInt(str));
            }
            for(int i=bitSet.nextSetBit(0);i>=0;i=bitSet.nextSetBit(i+1)){
                writer.write(i+"");
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("排序时间(s)："+(System.currentTimeMillis()-start)/1000);
    }

    static void get(String path,int from,int to){
        BufferedReader reader=null;
        try {
            reader=new BufferedReader(new FileReader(path));
            String str=null;
            for(int i=from;i<to&&(str=reader.readLine())!=null;i++){
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
